package us.gaaoc.framework.webapp.util;



import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

@Component
public class EmailSender implements Sender {
	final Logger logger = Logger.getLogger(EmailSender.class);
	
	
	private final JavaMailSender mailSender;

	@Autowired
	public EmailSender(JavaMailSender mailSender) {		
		this.mailSender = mailSender;
	}

	/* (non-Javadoc)
	 * @see com.paypal.vms.util.Sender#send(org.springframework.mail.SimpleMailMessage, java.lang.String, java.util.Map)
	 */
	@Override
	public void send(final SimpleMailMessage msg,final String attachmentPath,final String attachmentName,final String body) {
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			
			@Override
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
				message.setTo(msg.getTo());
				message.setFrom(msg.getFrom());
				if(msg.getBcc()!=null&&msg.getBcc().length>0)
					message.setBcc(msg.getBcc());
				if(msg.getCc()!=null&&msg.getCc().length>0)
					message.setCc(msg.getCc());
				message.setSubject(msg.getSubject());
				logger.info("body={}"+ body);

				message.setText(body, true);
				 if (!StringUtils.isBlank(attachmentPath)) {
	                    FileSystemResource file = new FileSystemResource(attachmentPath);
	                    message.addAttachment(attachmentName, file);
	                }
			}
		};

		mailSender.send(preparator);

		logger.info("Sent e-mail to '{}'."+ msg.getTo());
	}

    /* (non-Javadoc)
	 * @see com.paypal.vms.util.Sender#sendEmail(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
    @Override
	public void sendEmail(final String toEmailAddresses, final String fromEmailAddress,
                          final String subject,final String body) {
        sendEmail(toEmailAddresses, fromEmailAddress, subject,body, null, null);
    }

    /* (non-Javadoc)
	 * @see com.paypal.vms.util.Sender#sendEmailWithAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
    @Override
	public void sendEmailWithAttachment(final String toEmailAddresses, final String fromEmailAddress,
                                        final String subject,final String body, final String attachmentPath,
                                        final String attachmentName) {
        sendEmail(toEmailAddresses, fromEmailAddress, subject,body, attachmentPath, attachmentName);
    }

    private void sendEmail(final String toEmailAddresses, final String fromEmailAddress,
                           final String subject,final String body, final String attachmentPath,
                           final String attachmentName) {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                message.setTo(toEmailAddresses);
                message.setFrom(new InternetAddress(fromEmailAddress));
                message.setSubject(subject);                
                message.setText(body, true);
                if (!StringUtils.isBlank(attachmentPath)) {
                    FileSystemResource file = new FileSystemResource(attachmentPath);
                    message.addAttachment(attachmentName, file);
                }
            }
        };
        this.mailSender.send(preparator);
    }

}